package com.xiaolei.library.Utils;

/**
 * 任何数组的操作
 */
public class ArrayUtils
{
    /**
     * 头尾交换顺序的值
     *
     * @param array      数组
     * @param startIndex 起始的下标
     * @param endIndex   结束的下标
     */
    public static void reverse(byte[] array, int startIndex, int endIndex)
    {
        if (startIndex >= 0 && endIndex < array.length && endIndex > startIndex)
        {
            int len = endIndex - startIndex + 1;
            int half = len / 2;
            for (int i = 0; i < half; i++)
            {
                byte a = array[startIndex + i];
                byte b = array[endIndex - i];

                a = (byte) (a + b);
                b = (byte) (a - b);
                a = (byte) (a - b);

                array[startIndex + i] = a;
                array[endIndex - i] = b;
            }
        }
    }

    /**
     * 头尾交换顺序的值
     *
     * @param array      数组
     * @param startIndex 起始的下标
     * @param endIndex   结束的下标
     */
    public static void reverse(int[] array, int startIndex, int endIndex)
    {
        if (startIndex >= 0 && endIndex < array.length && endIndex > startIndex)
        {
            int len = endIndex - startIndex + 1;
            int half = len / 2;
            for (int i = 0; i < half; i++)
            {
                int a = array[startIndex + i];
                int b = array[endIndex - i];

                a = a + b;
                b = a - b;
                a = a - b;

                array[startIndex + i] = a;
                array[endIndex - i] = b;
            }
        }
    }

    /**
     * 头尾交换顺序的值
     *
     * @param array      数组
     * @param startIndex 起始的下标
     * @param endIndex   结束的下标
     */
    public static void reverse(short[] array, int startIndex, int endIndex)
    {
        if (startIndex >= 0 && endIndex < array.length && endIndex > startIndex)
        {
            int len = endIndex - startIndex + 1;
            int half = len / 2;
            for (int i = 0; i < half; i++)
            {
                short a = array[startIndex + i];
                short b = array[endIndex - i];
                a = (short) (a + b);
                b = (short) (a - b);
                a = (short) (a - b);
                array[startIndex + i] = a;
                array[endIndex - i] = b;
            }
        }
    }

    /**
     * 头尾交换顺序的值
     *
     * @param array      数组
     * @param startIndex 起始的下标
     * @param endIndex   结束的下标
     */
    public static void reverse(long[] array, int startIndex, int endIndex)
    {
        if (startIndex >= 0 && endIndex < array.length && endIndex > startIndex)
        {
            int len = endIndex - startIndex + 1;
            int half = len / 2;
            for (int i = 0; i < half; i++)
            {
                long a = array[startIndex + i];
                long b = array[endIndex - i];
                a = a + b;
                b = a - b;
                a = a - b;
                array[startIndex + i] = a;
                array[endIndex - i] = b;
            }
        }
    }


}
